import time

from PyQt5.QtCore import QThread, pyqtSignal
from common.common import Common


class WorkThread(QThread):
    signal_update = pyqtSignal(object)

    def __init__(self, connect_instance):
        super(WorkThread, self).__init__()
        self._bloop = False
        self.connect_instance = connect_instance
        pass

    def __del__(self):
        pass

    def set_loop(self, bloop: bool):
        self._bloop = bloop

    def run(self):
        try:
            while Common().get_thread_recv():
                if Common().get_connect_type() == 'can':
                    msg = self.connect_instance.bus_recv()
                    #print(msg)
                    dict_data = self.connect_instance.can_data_parse(msg)
                    self.signal_update.emit(dict_data)
                    #time.sleep(0.05)
                elif Common().get_connect_type() == 'udp':
                    self.connect_instance.recv_udp_data()
                    time.sleep(0.05)
                    pass
                pass
        except Exception as inst:
            print(inst)
        pass
